# encoding = utf-8
import pickle
import time

from application.logging import logger
from application.model.outside_temperature.get_model_path_by_model_name import get_model_path_by_model_name
from application.utils.CodeTimingUtil import CodeTimingUtil


@CodeTimingUtil(name="补齐天气数据[Temperature_Missing_value]")
def Temperature_Missing_value(model_name_temp, model_name_humidity, model_name_weather, model_name_wind, station_data):
    """
    天气数据补齐
    :param model_name_temp:
    :param model_name_humidity:
    :param model_name_weather:
    :param model_name_wind:
    :param station_data:
    :return:
    """
    # ==================================================================================================================
    # 加载天气模型
    # ==================================================================================================================
    # try: ->
    try:
        # 读取模型: 温度
        model_temp = pickle.load(open(get_model_path_by_model_name(model_name=model_name_temp), "rb"))
        logger.warning(f"加载天气模型[温度]: {model_name_temp}")
        # 读取模型: 湿度
        model_humidity = pickle.load(open(get_model_path_by_model_name(model_name=model_name_humidity), "rb"))
        logger.warning(f"加载天气模型[湿度]: {model_name_humidity}")
        # 读取模型: 天气
        model_weather = pickle.load(open(get_model_path_by_model_name(model_name=model_name_weather), "rb"))
        logger.warning(f"加载天气模型[天气]: {model_name_weather}")
        # 读取模型: 风速
        model_wind = pickle.load(open(get_model_path_by_model_name(model_name=model_name_wind), "rb"))
        logger.warning(f"加载天气模型[风速]: {model_name_wind}")
        pass  # <- try:

    # ==================================================================================================================
    # 加载天气模型:失败
    # ==================================================================================================================
    except BaseException as err:  # except ->
        logger.error(f"加载天气模型:失败: {err}")
        return
        pass  # <- except

    # ==================================================================================================================
    # 加载天气模型:成功
    # ==================================================================================================================
    logger.info(f"加载天气模型:成功")
    # try:读取字段[collect_time] ->
    try:
        time_string = station_data["collect_time"]
        logger.info(f"time_string: \n {time_string}")
        pass
    except KeyError:
        logger.error(f"没有这个索引:collect_time")
        return
        pass
    #
    #
    temperature_list = []
    humidity_list = []
    weather_list = []
    wind_list = []
    #
    # try: ->
    try:
        # for 循环时间 -》
        for _ in time_string:
            # 时间解析
            ans_time = time.strptime(str(_), "%Y-%m-%d %H:%M:%S")
            # 时间转换:时间戳
            time_stamp = float(time.mktime(ans_time))
            # 补齐温度
            temperature = model_temp(time_stamp)
            # 补齐湿度
            humidity = model_humidity(time_stamp)
            # 补齐天气
            weather = model_weather(time_stamp)
            # 补齐风速
            wind = model_wind(time_stamp)
            # 加入列表
            temperature_list.append(int(temperature))
            humidity_list.append(int(humidity))
            weather_list.append(int(weather))
            wind_list.append(int(wind))
            pass  # for 循环时间 《-
        pass  # <- try:
    except RuntimeError as err:  # except ->
        logger.error(f"室外天气补齐, 解析时间的for循环出错")
        pass  # <- except
    else:  # else ->
        # 数据补齐:data
        station_data["outside_temper"] = temperature_list
        station_data["outside_humidity"] = humidity_list
        station_data["outside_wind"] = wind_list
        station_data["outside_weather"] = weather_list
        #
        logger.info(f"补齐天气数据完成[station_data]:\n {station_data}")
        return station_data
        pass  # <- else
    pass  # <- Temperature_Missing_value


pass

if __name__ == '__main__':
    Temperature_Missing_value()
    pass
